﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WarehouseInsp.Buz.Interfaces;
using Microhard.Core.DBUtility;
using Microhard.WarehouseInsp.Buz.DataAccess;
using Microhard.WarehouseInsp.Buz.DTO;
using Microhard.Core;
using Microhard.WarehouseInsp.Buz.Msg;
using Microhard.WarehouseInsp.Buz.Models;
using Microhard.WarehouseInsp.Buz.BuzEnum;

namespace Microhard.WarehouseInsp.Buz.Impls
{
    public class WarehouseTransitionImpl : IWarehouseTransition
    {

        public Microhard.Core.DBUtility.ExecutedResult Add(WarehouseTransitionModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            WarehouseInspEntities db = new WarehouseInspEntities();
            var entity = db.Warehouses.SingleOrDefault(l => l.WarehouseID == model.WarehouseID);

           

            model.WarehouseName = entity.Name;
            model.TransEmployeeID = (Guid)entity.WarehouseMngID;
            model.TransEmployeeName = entity.WarehouseMngName;
            model.CreateTime = DateTime.Now;
            model.CreatorID = scope.CurrentUserID.ToString();
            model.CreatorName = scope.CurrentUserName;

            var op = model.ToEntity();
            try
            {
                db.WarehourseTransitions.AddObject(op);
                entity.WarehouseMngID = model.RecEmployeeID;
                entity.WarehouseMngName = model.RecEmployeeName;

                db.SaveChanges();

            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, WarehouseMsg.AddFailed);
                Log.Error("IWarehourseTransition.Add", ex);
            }

            return rtn;
        }

        public Microhard.Core.DBUtility.ExecutedResult Update(WarehouseTransitionModel model)
        {
            ExecutedResult result = new ExecutedResult();
            WarehouseInspEntities db = new WarehouseInspEntities();
            try
            {
                var entity = db.WarehourseTransitions.SingleOrDefault(l => l.WarehourseTransitionID == model.WarehourseTransitionID);
                if (entity != null)
                {
                    entity.RecEmployeeID = model.RecEmployeeID;
                    entity.RecEmployeeName = model.RecEmployeeName;
                    entity.Check1 = model.Check1;
                    entity.Check2 = model.Check2;
                    entity.Check3 = model.Check3;
                    entity.Check4 = model.Check4;
                    entity.Check5 = model.Check5;
                    entity.Check6 = model.Check6;
                    entity.Check7 = model.Check7;
                    entity.Check8 = model.Check8;
                    entity.Check9 = model.Check9;
                    entity.Check10 = model.Check10;
                    entity.Check11 = model.Check11;
                    entity.Check12 = model.Check12;
                    entity.Check13 = model.Check13;
                    entity.Check14 = model.Check14;
                    entity.Check15 = model.Check15;
                    entity.Check16 = model.Check16;
                    entity.Check18 = model.Check18;
                    entity.Check19 = model.Check19;
                    entity.Check20 = model.Check20;

                    var entity2 = db.Warehouses.SingleOrDefault(l => l.WarehouseID == entity.WarehouseID);
                    entity2.WarehouseMngID = model.RecEmployeeID;
                    entity2.WarehouseMngName = model.RecEmployeeName;

                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, WarehouseMsg.UpdateFailed);
                Log.Error("IWarehourseTransition.Update", ex);
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            WarehouseInspEntities db = new WarehouseInspEntities();
            var entity = db.WarehourseTransitions.Where(l => ids.Contains(l.WarehourseTransitionID));
            foreach (var e in entity)
            {

                try
                {
                    db.WarehourseTransitions.DeleteObject(e);
                }
                catch
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, "无法删除");
                }
            }
            if (result.ErrorMsg.Count == 0)
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, WarehouseMsg.DeleteFailed);
                    Log.Error("IWarehourseTransition.Delete", ex);
                }
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(WarehouseTransitionModel model)
        {
            throw new NotImplementedException();
        }

        public WarehouseTransitionModel GetModelByID(Guid id)
        {
            WarehouseInspEntities db = new WarehouseInspEntities();
            var mcmp = db.WarehourseTransitions.SingleOrDefault(c => c.WarehourseTransitionID == id);
            if (mcmp != null)
            {
                return mcmp.ToModel();
            }
            return null;
        }

        public WarehouseTransitionModel GetModelByModel(WarehouseTransitionModel model)
        {
            throw new NotImplementedException();
        }

        public List<WarehouseTransitionModel> Query(WarehouseTransitionModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            WarehouseInspEntities db = new WarehouseInspEntities();

            IEnumerable<WarehourseTransition> entities = from b in db.WarehourseTransitions where b.WarehouseID == Model.WarehouseID select b;


            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            if (pagedEntities != null && pagedEntities.Count > 0)
                return pagedEntities.Select(m => m.ToModel()).ToList();
            return new List<WarehouseTransitionModel>();
        }


        public List<WarehouseTransitionModel> Query(WarehouseTransitionModel Model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
